home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / time / mktime.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  103 lines

  1.  
  2. /*
  3.  *  MKTIME.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <time.h>
  11. #include <lib/misc.h>
  12.  
  13. time_t
  14. mktime(tm)
  15. struct tm *tm;
  16. {
  17.     static int mon[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  18.     struct tm tm_tmp;
  19.     time_t  t;
  20.     short notLeap;
  21.     short i;
  22.  
  23.     /*
  24.      *    1976 was a leap year, take quad years from 1976, each
  25.      *    366+365+365+365 days each.  Then adjust for the year
  26.      */
  27.  
  28.     t = ((tm->tm_year - 76) / 4) * ((366 + 365 * 3) * 86400);
  29.  
  30.     /*
  31.      *    compensate to make it work the same as unix time (unix time
  32.      *    started 8 years earlier)
  33.      */
  34.  
  35.     t += _TimeCompensation;
  36.  
  37.     /*
  38.      *    take care of the year within a four year set, add a day for
  39.      *    the leap year if we are based at a year beyond it.
  40.      */
  41.  
  42.     t += ((notLeap = (tm->tm_year - 76) % 4)) * (365 * 86400);
  43.  
  44.     if (notLeap)
  45.     t += 86400;
  46.  
  47.     /*
  48.      *    calculate days over months then days offset in the month
  49.      */
  50.  
  51.     for (i = 0; i < tm->tm_mon; ++i) {
  52.     t += mon[i] * 86400;
  53.     if (i == 1 && notLeap == 0)
  54.         t += 86400;
  55.     }
  56.     t += (tm->tm_mday - 1) * 86400;
  57.  
  58.     /*
  59.      *    our time is from 1978, not 1976
  60.      */
  61.  
  62.     t -= (365 + 366) * 86400;
  63.  
  64.     /*
  65.      *    calculate hours, minutes, seconds
  66.      */
  67.  
  68.     t += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  69.  
  70.     localtime_tm(&t, &tm_tmp);
  71.     tm->tm_wday = tm_tmp.tm_wday;
  72.     tm->tm_yday = tm_tmp.tm_yday;
  73.  
  74.     return(t);
  75. }
  76.  
  77. #ifdef TEST
  78.  
  79. #include <stdio.h>
  80.  
  81. main(ac, av)
  82. char *av[];
  83. {
  84.     struct tm tm;
  85.     time_t t;
  86.  
  87.     if (ac < 4) {
  88.     puts("test day mo yr");
  89.     exit(0);
  90.     }
  91.     clrmem(&tm, sizeof(tm));
  92.     tm.tm_mday = atoi(av[1]);
  93.     tm.tm_mon  = atoi(av[2]) - 1;
  94.     tm.tm_year = atoi(av[3]) - 1900;
  95.  
  96.     t = mktime(&tm);
  97.  
  98.     printf("%08lx %s", t, ctime(&t));
  99.     return(0);
  100. }
  101.  
  102. #endif
  103.